<script setup>
import { ref } from 'vue'
import { getLogin } from '@/api/login'
import { MessagePlugin } from 'tdesign-vue-next'
import { useRouter } from 'vue-router'
import { setToken } from '@/utils/storage'

const router = useRouter()
const form = ref({
    username: 'demo',
    password: 'zh@hm#23'
})
const rules = ref({
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
        { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' },
    ]
})
const remember = ref(true)

const login = async () => {
    const res = await getLogin(form.value)
    if (res.code === 10000) {
        await MessagePlugin.success('登录成功！')
        await setToken(res.data.token)
        await router.push('/dashboard')
    }
}
</script>

<template>
    <div class="login">
        <div class="bg"></div>

        <div class="left">
            <div class="main">
                <h1>智慧园区-登录</h1>
                <t-form class="form" labelAlign="top" :rules="rules" :data="form">
                    <t-form-item label="账号" name="username">
                        <t-input placeholder="请输入账号" v-model.trim="form.username"/>
                    </t-form-item>
                    <t-form-item label="密码" name="password">
                        <t-input placeholder="请输入密码" v-model.trim="form.password" type="password"/>
                    </t-form-item>
                </t-form>
                <t-checkbox class="remember" v-model="remember">记住我</t-checkbox>
                <t-button block class="btn" @click="login">确定</t-button>
                <p class="tips">仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！</p>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.login {
    display: flex;

    .bg {
        width: 60vw;
        height: 100vh;
        background-image: url(https://fe-hmzs.itheima.net/assets/login-bg-e4ea539c.svg);
        background-repeat: no-repeat;
        background-size: cover;
        background-position: right;
    }

    .left {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;

        .main {
            width: 50%;
            margin: 0 auto;
            padding-bottom: 100px;

            h1 {
                color: var(--td-text-color-primary);
                font-size: 26px;
                font-weight: 500;
                margin-bottom: 60px;
            }

            ::v-deep .form {
                margin-bottom: 20px;

                .t-form-item__username {
                    margin-bottom: 30px;
                }

                .t-form__label {
                    color: var(--td-text-color-secondary);
                    font-weight: 500;
                    font-size: 16px;
                }
            }

            .remember {
                margin-bottom: 10px;
            }

            .btn {
                margin: 30px 0;
            }

            .tips {
                font-size: 14px;
                color: var(--td-error-color);
            }
        }
    }
}

</style>
